//
//  ShoppingGoodsViewController.m
//  EasyToo
//
//  Created by zhengzhe on 13-9-14.
//
//

#import "ShoppingGoodsViewController.h"
#import "GoodsDetailViewController.h"
#import "ShoppingManager.h"
#import "Goods.h"
#import "UIImageView+OnlineImage.h"
#import "NetworkAlertView.h"

@interface ShoppingGoodsViewController ()

@end

@implementation ShoppingGoodsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
     [self.view setBackgroundColor:BackgroundColor];
    [self createTableView];
    
    [[ShoppingManager shareManager] setDelegate:self];
    if (!self.goodsArray || [self.goodsArray count] == 0)
    {
        [[NetworkAlertView shareView] showNetView];
        [[ShoppingManager shareManager] getCategoryGoods:self.categoryId];
    }
    
}

- (void)dealloc
{
    [[ShoppingManager shareManager] setDelegate:nil];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight] - 100) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [self.view addSubview:_tableView];
}

#pragma mark tableView datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.goodsArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    NSString *reuseIdentifier = @"shopCategoryCell";
    //
    //    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    //
    //    if (!cell)
    //    {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    
    //加载图像
    UIImageView *imgview  = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"0.jpg"]]autorelease];
    imgview.frame = CGRectMake(10, 10, 100, 100);
    imgview.backgroundColor = [UIColor whiteColor];
    [cell addSubview:imgview];
    
    //加载名字
    UILabel *namelbl = [[[UILabel alloc]initWithFrame:CGRectMake(120, 15, 160, 50)]autorelease];
    namelbl.font = [UIFont boldSystemFontOfSize:15];
    namelbl.textColor = [UIColor darkGrayColor];
    namelbl.lineBreakMode = UILineBreakModeTailTruncation;
    namelbl.numberOfLines = 0;
    namelbl.adjustsFontSizeToFitWidth = YES;
    [cell addSubview:namelbl];
    
    //加载描述
    UILabel *desclbl = [[[UILabel alloc]initWithFrame:CGRectMake(120, 75, 160, 20)]autorelease];
    desclbl.font = [UIFont boldSystemFontOfSize:18];
    desclbl.textColor = [UIColor redColor];
    desclbl.lineBreakMode = UILineBreakModeWordWrap;
    desclbl.numberOfLines = 0;
    [cell addSubview:desclbl];
    
    //    }
    Goods *goodsInfo = [self.goodsArray objectAtIndex:indexPath.row];
    
    namelbl.text = goodsInfo.name;
    desclbl.text = [NSString stringWithFormat:@"￥%.2f", goodsInfo.salePrice];
    [imgview setOnlineImage:goodsInfo.imgUrl];
    
    return cell;
}

#pragma mark tableView delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Goods *goodsInfo = [self.goodsArray objectAtIndex:indexPath.row];
    
    GoodsDetailViewController *goodsDetailCtrl = [[GoodsDetailViewController alloc] init];
    goodsDetailCtrl.goodsInfo = goodsInfo;
    [self.navigationController pushViewController:goodsDetailCtrl animated:YES];
    [goodsDetailCtrl release];
}

- (void)onGetCategoryGoods:(BOOL)isSuccess
                GoodsArray:(NSMutableArray*)goods
{
    
    [[NetworkAlertView shareView] delView];
    self.goodsArray = goods;
    [_tableView reloadData];
}

@end
